package com.yixing.demo.controller;

import com.yixing.demo.model.Message;
import com.yixing.demo.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageService messageService;
    @RequestMapping("/getmessage")
    public Object getMessage(int sessionId) {
        List<Message> messages = messageService.getMessagesBySessionId(sessionId);
        // 针对查询结果, 进行逆置操作. 因为界面上需要的是按照时间升序排列的消息. 此处得到的是降序排列的消息.
        for (int i = 0; i < messages.size(); i++) {
            System.out.println(messages.get(i).toString());
        }
        Collections.reverse(messages);
        return messages;
    }
}
